#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  contextfile.py
#       Author @  Huoty
#  Create date @  2015-08-08 17:02:13
#  Description @  
# *************************************************************

filename = 'my_file.txt'
mode = 'w' # Mode that allows to write to the file
writer = open(filename, mode)

class PypixOpen(object):
    def __init__(self, filename, mode):
        self.filename = filename
        self.mode = mode
 
    def __enter__(self):
        self.openedFile = open(self.filename, self.mode)
        return self.openedFile
 
    def __exit__(self, *unused):
        self.openedFile.close()

# Script starts from here
 
with PypixOpen(filename, mode) as writer:
    writer.write("Hello World from our new Context Manager!")
